// pages/Personal_matches/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    sponsorlogoes: [],
    logoUrl: [],  
    latitude: "",
    longitude: "",
    page: 1,
    type_id: "all",
    ordertype:1,
    personal_matches:[],
    isbottom:false,
    search:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sponsorlogoes: app.globalData.sponsorlogoes,
      logoUrl: app.globalData.imagelogourl,
    });
    this.getpersonalmatcheslists();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
      var userInfo = app.globalData.userInfo;
      this.setData({userInfo});
      var location =  wx.getStorageSync('location');
       if(location && location.lng && location.lat ){
          this.setData({latitude:location.lng,longitude:location.lat})
       }
       else{
         var that= this;
        /* wx.getLocation({
           type: 'wgs84',
           success: (result) => {
             const latitude = result.latitude
             const longitude = result.longitude
             const speed = result.speed
             const accuracy = result.accuracy
             that.setData({
               longitude: longitude,
               latitude: latitude
             });
             wx.setStorageSync('location', {lng:longitude,lat:latitude});
 
           },
           fail: (res) => {},
           complete: (res) => {},
         })*/
       } 
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getpersonalmatcheslists();
  },

  

  inputSearch: function (e) {
    this.setData({
      search: e.detail.value
    });
  },

  changeRules:function(e){
     this.setData({ordertype:e.currentTarget.dataset.index,personal_matches:[],isbottom:false,page:1});
     this.getpersonalmatcheslists();
  },

  searchF:function(){
    this.setData({personal_matches:[],isbottom:false});
    this.getpersonalmatcheslists();
  },

  getpersonalmatcheslists: function () {
    var params = {};
    let{type_id,search,page,ordertype,latitude,longitude,personal_matches,isbottom} = this.data;
    params.type_id =type_id;
    params.search = search;
    params.page = page;
    params.order = ordertype;
    params.latitude = latitude;
    params.longitude = longitude;
    wx.showNavigationBarLoading();
    var that = this;
    app.getpersonalmatcheslists(params, function (res) {
        console.log(res);
        if(res.data.data && res.data.data.length>0){
            if(personal_matches.length>0 ){
              personal_matches = personal_matches.concat(res.data.data);
            
            }
            else{
              personal_matches = res.data.data;
            }
            page++;
        }
        else {
          if(personal_matches.length>0 ){
            isbottom = true;
          }
        }
        that.setData({personal_matches,page,isbottom});
    },
      function () {
        wx.hideNavigationBarLoading();
      })
  },

  _openpersonalmatch:function(e) {
   
      var matchplay = e.currentTarget.dataset.personalmatches;
      wx.navigateTo({
          url: './match/match?match_id=' + matchplay.match_id,
      })

  },

  creatematches:function(){
      var userInfo = app.globalData.userInfo;
      console.log(userInfo);
      var title = "";
      var page ="";
      if(!userInfo || !userInfo.openid  ){
         title = "请先登录之后后才能创建球局";
         page = "/pages/login/login";
      }

      if (userInfo && userInfo.openid &&  (!userInfo.trueName|| !userInfo.player_id)){
        title = "注册球员信息才能创建球局";
        page = "/pages/More/information/information";
      }

      if(title){
      wx.showModal({
        title: title,
        cancelText:'再看看',
        confirmText:'OK',
        success:function(res){
            if(res.confirm){
                wx.navigateTo({
                  url: page,
                })
            }
            else if(res.cancel){

            }
        }
      })
     }
     else{
        wx.navigateTo({
          url: '../Personal_matches/createpersonalmatch/create',
        })
     }
  }

})